package longrise.phone.com.bjjt_henan.widget;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import longrise.phone.com.bjjt_henan.R;

/**
 * Created by Administrator on 2016/5/19.
 */
public class SelectDialog extends Dialog {

    private TextView cancelTV;

    private TextView confirmTV;

    private TextView titleTV;

    private ListView mListView;

    private onSelectListener listener;

    public SelectDialog(Context context, List<String> datas) {
        super(context, R.style.AlertDialogStyle);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_select, null);
        initViews(view);
        setContentView(view);
        //设置大小
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.80);
        dialogWindow.setAttributes(lp);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                R.layout.item_list_single_choice, R.id.checktv1, datas);
        mListView.setAdapter(adapter);
        setEvent();
    }

    private void initViews(View view) {
        titleTV = (TextView) view.findViewById(R.id.dialog_tv_title);
        cancelTV = (TextView) view.findViewById(R.id.dialog_tv_cancel);
        confirmTV = (TextView) view.findViewById(R.id.dialog_tv_confirm);
        mListView = (ListView) view.findViewById(R.id.dialog_lv);
    }

    public void setTitle(CharSequence title) {
        this.titleTV.setText(title);
    }

    private void setEvent() {
        mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (null != listener) {
                    listener.onCancel();
                }
            }
        });
        confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onSelect(mListView.getCheckedItemPosition());
                }
            }
        });
    }

    @Override
    public void show() {
        if (mListView != null) {
            mListView.clearChoices();
        }
        super.show();
    }

    public void setOnSelectListener(onSelectListener listener) {
        this.listener = listener;
    }

    public interface onSelectListener {

        void onCancel();

        void onSelect(int select);
    }

}
